#include <iostream>
#include <vector>
#include <numeric>
using namespace std;

using vec = vector<int>;

int find(vec &f, int x)
{
        return f[x] == x ? x : f[x] = find(f, f[x]);
}

int merge(vec &f, vec &s, int x, int y)
{
        int fx = find(f, x);
        int fy = find(f, y);
        if(fy == fx){
                return 0;
        }
        s[fy] += s[fx];
        return f[fx] = fy;
}

int main()
{
        int n = 0, m = 0, x = 0, y = 0, p = 0;
        char r;
        scanf("%d%d", &n, &m);

        vec father(n + 1, 0);
        vec size(n + 1, 1);
        iota(father.begin(), father.end(), 0);

        for(int i = 0; i < m; i++){
                scanf("%c", &r);
                scanf("%c", &r);
                if(r == 'M'){
                        scanf("%d%d", &x, &y);
                        merge(father, size, x, y);
                }else{
                        scanf("%d", &x);
                        p = find(father, x);
                        printf("%d\n", size[p]);
                }
        }
        return 0;
}